<?php


$connection = mysql_connect('localhost', 'root', 'usbw');
mysql_select_db('botrax', $connection);


$where_condition = ' AND (';
if (sizeof($_GET) > 0) {

    $action = $_GET['action'];
    $searchtext = $_GET['search'];

    if($action == "search" && $searchtext){

        $where_condition.= " description LIKE '%{$searchtext}%' OR tag = '%{$searchtext}%'";

    }else if( $action == "tag" && $searchtext ){

        $where_condition.= " tag = '{$searchtext}'";

    }else{
        return;
    }

    $where_condition.= ( $where_condition === ' AND (' ? '1)' : ')' );


    $formdatas = array();
    $result = mysql_query("SELECT description, formUrl,title, tag, sectag FROM formdata WHERE 1 {$where_condition}");

    while ($row = mysql_fetch_array($result)) {
        $formdatas[]= array(
            'description' => $row['description'],
            'formUrl' => $row['formUrl'],
            'title' => $row['title'],
            'tag' => $row['tag'],
            'sectag' => $row['sectag']
        );
    }
    echo json_encode($formdatas);

}



//echo $where_condition;
